package com.tbit.uqbike.webmanager.controller;

import com.tbit.uqbike.constant.UserConstant;
import com.tbit.uqbike.enums.SystemCodeEnum;
import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.object.pojo.Machine;
import com.tbit.uqbike.object.pojo.Park;
import com.tbit.uqbike.object.pojo.User;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.uqbike.service.base.MachineService;
import com.tbit.uqbike.service.business.AccountService;
import com.tbit.uqbike.service.business.ParkService;
import com.tbit.uqbike.service.business.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 中途停车记录
 *
 * @author Leon
 * 2017年2月24日 下午5:51:05
 */
@Validated
@RestController
@RequestMapping(value = "/park", produces = BaseController.MEDIA_JSON_UTF8)
public class ParkController extends BaseController {
    @Autowired
    private ParkService parkService;
    @Autowired
    private UserService userService;
    @Autowired
    private MachineService machineService;
    @Autowired
    private AccountService accountService;

    /* 分页查询 */
    @RequestMapping("/queryPage")
    public JsonResult queryPage(@RequestParam(defaultValue = "10") Integer rowCount,
                                @RequestParam(defaultValue = "1") Integer pageNO,
                                Integer userId, String phone, String userCode,
                                Long startTime, Long endTime,
                                HttpServletRequest request) {
        // 获取用户信息
        LoginInfo loginInfo = getLogin(request);
        Integer accountId;
        if (loginInfo.type.equals(UserConstant.USER_ADMIN)) {
            accountId = null;
        } else if (loginInfo.type.equals(UserConstant.USER_BRAND)) {
            accountId = loginInfo.id;
        } else {
            accountId = accountService.getBrandId(loginInfo.id);
        }

        List<Integer> userIds = new ArrayList<>();
        if (phone != null) {
            List<User> users = userService.getByAccountIdAndPhone(accountId, phone);
            if (users == null || users.size() < 1) {
                throw new BaseException(SystemCodeEnum.USER_NOT_EXIT);
            } else {
                for (User user : users) {
                    userIds.add(user.getUserId());
                }
            }
        }
        Integer machineId = null;
        if (userCode != null && userCode.length() > 0) {
            Machine machine = machineService.getByUserCode(userCode);
            if (machine == null) {
                throw new BaseException("usercode_not_exist");
            } else {
                machineId = machine.getMachineId();
            }
        }

        /*获取总数*/
        Integer total = parkService.queryCount(userIds, machineId, startTime, endTime);
        /*当前返回行数*/
        List<Park> parkLogs = parkService.queryPage(userIds, machineId, startTime, endTime, rowCount, pageNO);

        return JsonResult.ok(parkLogs, total);
    }

}